﻿using System;
using System.Web;
using System.Web.Security;

namespace good1.Web.Controllers.Utility
{
    [Obsolete("Please look to GoodCookies to use cookies, this is only still here for Login Authentication which is still difficult to do with container.")]
    public static class CookieHelper
    {

        /// <summary>
        /// Damned but I can't get this done using the container.  TODO colum, come back and do it there, but it seems to demand the HttpContext not just the current. 
        /// </summary>
        /// <param name="contextBase"></param>
        /// <param name="username"></param>
        /// <param name="roles"></param>
        /// <param name="rememberMe"></param>
        public static void WriteAuthTicketToCookie(HttpContextBase contextBase, string username, string roles,  bool rememberMe)
        {
            //TODO come back to line below but it's a right pain and seen many others with the same even http://chwe.at/blog cheats in his CookieContainerApp
            //goodCookies.SetAuthenticationTicket(howLongToRember, FormsAuthentication.FormsCookieName, loginForm.Email, roles);

            //FormsAuthentication.SetAuthCookie(loginForm.Email, loginForm.RememberMe);
            //above would be a simple way to add auth to cookie but does not allow for the roles to be stored as below. 
            
            // Create and tuck away the cookie
            FormsAuthenticationTicket authTicket =
              new FormsAuthenticationTicket(1,
                                            username,
                                            DateTime.Now,
                                            DateTime.Now.AddMinutes(60),
                                            rememberMe,
                                            roles);
            string encTicket = FormsAuthentication.Encrypt(authTicket);
            HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            authenticationCookie.HttpOnly = true;  //http://www.codinghorror.com/blog/archives/001167.html
            contextBase.Response.Cookies.Add(authenticationCookie);
        }

     
    }
}
